package zhongbo.java.air.dictionary.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.dictionary.exception.BusinessException;
import zhongbo.java.air.dictionary.exception.ExceptionCode;
import zhongbo.java.air.dictionary.mapper.AirPortMapper;
import zhongbo.java.air.dictionary.model.AirPort;
import zhongbo.java.air.dictionary.service.IAirPortService;
import zhongbo.java.air.dictionary.vo.PageVo;

@Service
public class AirPortServiceImpl implements IAirPortService {
    @Autowired
    AirPortMapper airPortMapper;

    @Override
    public int addAirPort(AirPort airPort) {
        airPort.setId(null);
        if (airPort == null && airPort.equals("")){
            throw new BusinessException(ExceptionCode.AIRPORT_NULL);
        }
        int add = airPortMapper.insert(airPort);
        return add;
    }

    @Override
    public void deleteAirPort(String id) {
        if (id ==null && id.equals("")){
            throw new BusinessException(ExceptionCode.ID_NULL);
        }
        airPortMapper.deleteById(id);
    }

    @Override
    public int updateAirPort(AirPort airPort) {
        if (airPort == null && airPort.equals("")){
            throw new BusinessException(ExceptionCode.AIRPORT_NULL);
        }

        int update = airPortMapper.updateById(airPort);

        return update;
    }

    @Override
    public AirPort selectById(String id) {
        if (id == null && id.equals("")){
            throw new BusinessException(ExceptionCode.ID_NULL);
        }
        AirPort airPort = airPortMapper.selectById(id);
        return airPort;
    }

    @Override
    public PageVo page(String name, Integer pageNumber, Integer pageSize) {
        QueryWrapper<AirPort> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(name)){
            queryWrapper.like("name",name);
        }
        IPage<AirPort> pageParam = new Page<>(pageNumber,pageSize);
        IPage<AirPort> AirIPage = airPortMapper.selectPage(pageParam,queryWrapper);
        return new PageVo<>(AirIPage);
    }


}
